﻿using Rug.Osc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PlayListSet.Comm
{
    internal class OSCSendControl
    {
        private System.Threading.Timer timer;
        public bool OSCSend { set; get; } = false;
        public System.Net.IPEndPoint OSCSendNetInfo { set; get; }
        public System.Net.IPEndPoint LocalNetInfo { set; get; }

        public Rug.Osc.OscSender OSCSendSender { set; get; }
        public Timer jishu;
        public OSCSendControl()
        {
            OSCSend =AppControl.Instance.AppConfig.OSCSend;
            OSCSendNetInfo = VCLib.Converts.IPConverts.ConverToIPEndPoint(AppControl.Instance.AppConfig.OSCSendNetInfo);
            LocalNetInfo = VCLib.Converts.IPConverts.ConverToIPEndPoint(AppControl.Instance.AppConfig.OSCLocalNetInfo);

            OSCSendSender = new OscSender(
                LocalNetInfo.Address, LocalNetInfo.Port,
                OSCSendNetInfo.Address,
                 OSCSendNetInfo.Port,8,600,2048);
            timer = new System.Threading.Timer(ThreadingTimer_Elapsed,null,0,33);
            //jishu = new System.Threading.Timer(jishuElapsed, null, 0, 1000);
            //timer.Elapsed += Timer_Elapsed;
        
        }
        private void jishuElapsed(object state)
        {
            //if(AppControl.Instance.TimeCodePlayContorl.PlaybackState== NAudio.Wave.PlaybackState.Playing)
            // {
            if (!IsOpen)
                return;
            var a = count;
           count = 0;
            Console.WriteLine(  a);
            //}
            //else
            //{
            //    OSCSendSender.Send(new Rug.Osc.OscMessage("/timeCode",
            //      (int)AppControl.Instance.TimeCodePlayContorl.PlaybackState,
            // AppControl.Instance.TimeCodePlayContorl.CurrentTime.TotalSeconds));
            //}

        }
        public bool IsOpen = false;
        private void ThreadingTimer_Elapsed(object state)
        {
            //if(AppControl.Instance.TimeCodePlayContorl.PlaybackState== NAudio.Wave.PlaybackState.Playing)
            // {
            if (!IsOpen)
                return;
            OSCSendSender.Send(new Rug.Osc.OscMessage("/timeCode",
                (int)AppControl.Instance.TimeCodePlayContorl.PlaybackState,
           AppControl.Instance.TimeCodePlayContorl.GetCurrentTime().TotalSeconds,
            AppControl.Instance.AppConfig.Vol
           ));
            count++;
            //}
            //else
            //{
            //    OSCSendSender.Send(new Rug.Osc.OscMessage("/timeCode",
            //      (int)AppControl.Instance.TimeCodePlayContorl.PlaybackState,
            // AppControl.Instance.TimeCodePlayContorl.CurrentTime.TotalSeconds));
            //}

        }
        int count = 0;
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
           //if(AppControl.Instance.TimeCodePlayContorl.PlaybackState== NAudio.Wave.PlaybackState.Playing)
           // {

                OSCSendSender.Send(new Rug.Osc.OscMessage("/timeCode",
                    (int)AppControl.Instance.TimeCodePlayContorl.PlaybackState,
               AppControl.Instance.TimeCodePlayContorl.GetCurrentTime().TotalSeconds,
                   AppControl.Instance.AppConfig.Vol));
            //}
            //else
            //{
            //    OSCSendSender.Send(new Rug.Osc.OscMessage("/timeCode",
            //      (int)AppControl.Instance.TimeCodePlayContorl.PlaybackState,
            // AppControl.Instance.TimeCodePlayContorl.CurrentTime.TotalSeconds));
            //}
           
        }

        public void Open()
        {
            if (OSCSend)
            {
                OSCSendSender.Connect();
                IsOpen =true;
            }
           
        }
        public void Close() 
        {
            IsOpen = false;

        }
        public void Dispose()
        {
            IsOpen = false;
            timer.Dispose(); //jishu.Dispose();
        }
    }
}
